layui.config({
    base: config.base + 'layui-admin/'
}).extend({
    index: 'lib/index'
}).use(['index'], function () {
    const $ = layui.$;
    const admin = layui.admin;
    const layer = layui.layer;
    const table = layui.table;
    const laydate = layui.laydate;
    const util = layui.util;
    const fn = {
        //查询
        query: function () {
            tableRender.reload({
                where: {
                    startTime: fn.time1()
                    , endTime: fn.time2()
                    , userId: $('#userId').val()
                }
                , page: {
                    curr: 1
                }
            });
        }
        //删除
        , del: function (items) {
            layer.confirm('是否删除【' + items.length + '】条日志？', {
                title: '删除'
                , icon: 3
                , closeBtn: 0
                , skin: 'layui-layer-admin'
            }, function (index) {
                layer.close(index);
                admin.req({
                    url: '../../admin/logger/delete.json'
                    , contentType: "application/json;charset=UTF-8"
                    , type: 'delete'
                    , data: JSON.stringify(items)
                    , done: function (data) {
                        tableRender.reload();
                    }
                });
            });
        }
        , time: function (value) {
            return value ? new Date(new Date().getTime() + value) : new Date();
        }
        , time1: function (fmt) {
            return fmt ? util.toDateString(fn.time(), fmt)
                : ($('#createTime').val().split("~")[0] || '').trim();
        }
        , time2: function (fmt) {
            return fmt ? util.toDateString(fn.time(24 * 60 * 60 * 1000), fmt)
                : ($('#createTime').val().split("~")[1] || '').trim()
        }
    };
    const tableRender = table.render({
        id: 'logger-table'
        , elem: '#logger-table'
        , toolbar: '#logger-toolbar'
        , url: '../../admin/logger/list.json'
        , page: true
        , where: {
            startTime: fn.time1('yyyy-MM-dd')
            , endTime: fn.time2('yyyy-MM-dd')
        }
        , response: {
            statusName: 'code'
            , statusCode: 0
            , msgName: 'message'
            , countName: 'total'
            , dataName: 'data'
        }
        , cols: [
            [
                {type: 'checkbox'}
                , {field: 'userId', title: '用户', width: 80}
                , {field: 'ip', title: '地址', width: 120}
                , {field: 'os', title: '系统', width: 100}
                , {field: 'browser', title: '浏览器', width: 100}
                , {field: 'userAgent', title: '代理', hide: true}
                , {field: 'message', title: '信息'}
                , {field: 'costMillis', title: '耗时', align: 'right', width: 80, templet: '#millis-tpl'}
                , {field: 'createTime', title: '创建时间', width: 120}
                , {title: '操作', align: 'center', toolbar: '#logger-tool', width: 80}
            ]
        ]
    });
    //绑定工具栏事件
    table.on('toolbar(logger-table)', function (obj) {
        const items = table.checkStatus('logger-table').data;
        if (obj.event === 'query-toolbar') {
            fn.query();
        } else if (obj.event === 'del-toolbar') {
            if (items.length <= 0) {
                layer.msg("请选择一条纪录！");
            } else {
                const ids = [];
                layui.each(items, function (index, item) {
                    ids.push(item.id);
                });
                fn.del(ids);
            }
        } else if (obj.event === 'refresh-toolbar') {
            tableRender.reload();
        }
    });
    //绑定表格数据行操作事件
    table.on('tool(logger-table)', function (obj) {
        const item = obj.data;
        if (obj.event === 'del-tool') {
            fn.del([item.id]);
        }
    });
    laydate.render({
        elem: '#createTime'
        , range: '~'
        , btns: ['confirm']
        , value: fn.time1('yyyy-MM-dd') + ' ~ ' + fn.time2('yyyy-MM-dd')
    });
    $('#query-field').on('click', function () {
        fn.query();
    });
    $('#reset-query-field').on('click', function () {
        laydate.render({
            elem: '#createTime'
            , value: fn.time1('yyyy-MM-dd') + ' ~ ' + fn.time2('yyyy-MM-dd')
        });
        $('#userId').val('');
    });
    $('#more-query-field').on('click', function () {
        $("div[name='box-query-field']").toggle();
        $(this).html($(this).html() === '\n收起' ? '\n展开' : '\n收起')
            .toggleClass('layui-icon-down').toggleClass('layui-icon-up');
    });
});